//
//  MoveCarInputViewModel.m
//  carcareIOS
//
//  Created by 王然 on 16/9/18.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "MoveCarInputViewModel.h"
#import "ChoiceMoveCardViewController.h"
#import "carcareIOS-Swift.h"
#import "BaseWebViewController.h"
@interface MoveCarInputViewModel()

@property (nonatomic , copy) NSString * telStr;

@property (nonatomic , copy) NSString * brandStr;

@property (nonatomic , strong) MoveCarQRModel * createAndUpdateModel;
@end
@implementation MoveCarInputViewModel

 - (instancetype)init
{
    self = [super init];
    if (self) {
        
        self.createAndUpdateModel = [[MoveCarQRModel alloc]init];
        
    }
    return self;
}
//生成挪车卡按钮
- (void)clickOnCreateMoveCarCarTel:(NSString *)telStr brandCity:(NSString *)brandCity brandNum:(NSString *)brandNum
{
    self.telStr = telStr;
    self.brandStr = [NSString stringWithFormat:@"%@%@",brandCity, brandNum];
    
    if([Check isTel:telStr] && [Check isNumOrBigASCIILength:brandNum]){
        
        self.createAndUpdateModel.phone = telStr;
        self.createAndUpdateModel.carno = self.brandStr;
        if([((MoveCarInputViewController *)self.viewController).moveCarQRModel propertyDictionaryValue].count > 0)
        {
            self.createAndUpdateModel.id = ((MoveCarInputViewController *)self.viewController).moveCarQRModel.id;
            if([[((MoveCarInputViewController *)self.viewController).moveCarQRModel.styleId stringValue] isNotNil])self.createAndUpdateModel.styleId = ((MoveCarInputViewController *)self.viewController).moveCarQRModel.styleId;
        }
        ((MoveCarInputViewController *)self.viewController).moveCarQRModel.phone = telStr;
        ((MoveCarInputViewController *)self.viewController).moveCarQRModel.carno = self.brandStr;
        __weak __typeof(self) wself = self;
        [((MoveCarInputViewController *)self.viewController) clickOnBG];
        //注意：请求成功或失败都要设置okBtn 可以点击 ((MoveCarInputViewController *)wself.viewController).okButton.enabled = YES;
        [NetMoveCar getQRURLWithPhone:telStr carno:_brandStr success:^(NSString * _Nonnull url) {
            wself.createAndUpdateModel.tdCodeUrl = url;
            [wself loadURl:url styleIDUpdate:^{
            }];
            
            ((MoveCarInputViewController *)wself.viewController).okButton.enabled = YES;
        } fail:^{
            ((MoveCarInputViewController *)wself.viewController).okButton.enabled = YES;
        }];
          
    }
}
- (void)loadURl:(NSString *)url styleIDUpdate:(void(^)())update
{
    __weak __typeof(self) wself = self;
    ChoiceMoveCardViewController * vc = [[[ChoiceMoveCardViewController alloc] initWithMainViewType:TYPE_MAIN_VIEW_NORMAL] navSetupBackItemWithIdentification:BACK_ICON];
    if([url isNotNil]){
        [vc reloadMoveCarQRModel:self.createAndUpdateModel upDateStyleId:^(NSNumber * styleID) {
            wself.createAndUpdateModel.styleId = styleID;
            if(update)update();
        }];
    
//        [vc.imageV setImageWithURL:[NSURL URLWithString:url]];
    }
    [((MoveCarInputViewController *)self.viewController).navigationController pushViewController:vc animated:YES];
}
//挪车卡使用说明按钮
- (void)clickOnMoveCarInstructions
{
    [((MoveCarInputViewController *)self.viewController) clickOnBG];
    BaseWebViewController *webVC = [[BaseWebViewController alloc]initWithURL:@"http://r.ichezheng.com/cz/iomobile/www/app/pages/movecar.instruction.html"];
    [webVC navSetupLeftTitle:@"关闭" action:^{
        [webVC dismissViewControllerAnimated:YES completion:nil];
    }];
    [((MoveCarInputViewController *)self.viewController) presentViewController:[UINavigationController navigationControllerWithRootViewController:webVC] animated:YES completion:nil];
}
@end
